#include "app.h"

#include "OS_Task.h"

#include "dio.h"
#include "atd.h"
#include "led.h"
#include "pwm.h"



app::app() : bouton1(2), bouton2(3), poti1(5), poti2(6), bruiteur(5,7),
             frequence(0), volume(0), on(0)
{
  inputs[0] = &bouton1;
  inputs[1] = &bouton2;
  inputs[2] = &poti1;
  inputs[3] = &poti2;
}

void app::Init()
{
  dio_open();
  atd_open();
  led_open();
  pwm_open();

  on=0;
  
  (void)OS_ActivateTask(OS_TaskScan, 20, 10);
  (void)OS_ActivateTask(OS_TaskRefresh, 55, 100);
}

void app::ScanInputs()
{
  for(int i = 0;i<=3;i++)
    inputs[i]->scan();
}

void app::RefreshOutputs()
{	
/*	led_write(2,0);
	if(sonAproduire){
	 led_write(2,0);
	}else{
	  led_write(0,0);
	}*/
	if(!inputs[1]->state()){
	  on =0;
	}else if(!inputs[0]->state()){
	  on =1;
	}
	
	unsigned int volume = (inputs[3]->state()*100)/256;
	unsigned long frequence = (unsigned long)20 + (((unsigned long)inputs[2]->state()*(unsigned long)(20000-20))/(unsigned long)256);
	bruiteur.write( frequence , volume * on);
}
